package com.example.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.demo.entity.BaseBiz;
import com.example.demo.service.BaseBizService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class BaseBizServiceImpl implements BaseBizService {
    private final MongoTemplate mongoTemplate;
    @Override
    public void create(BaseBiz baseBiz) {
        if(Objects.isNull(baseBiz.getCreateTime()))
            baseBiz.setCreateTime(new Date());
        if(Objects.isNull(baseBiz.getUpdateTime()))
            baseBiz.setUpdateTime(new Date());
        if(Objects.isNull(baseBiz.getStateFlag()))
           baseBiz.setStateFlag(0);
        if(Objects.isNull(baseBiz.getDeleteFlag()))
            baseBiz.setDeleteFlag(false);
        mongoTemplate.save(baseBiz);
    }

    @Override
    public BaseBiz detail(String uuid) {
        return mongoTemplate.findOne(new Query().addCriteria(Criteria.where("id").is(uuid)), BaseBiz.class);
    }

    @Override
    public Page<BaseBiz> list(BaseBiz baseBiz, int page, int size) {
        Criteria criteria=new Criteria().and("deleteFlag").is(false);
        if(StringUtils.hasText(baseBiz.getTableName()))
            criteria=criteria.and("tableName").is(baseBiz.getTableName());
        if(StringUtils.hasText(baseBiz.getId()))
            criteria=criteria.and("id").is(baseBiz.getId());
        if(null!=baseBiz.getStateFlag())
            criteria=criteria.and("stateFlag").is(baseBiz.getStateFlag());
        if(StringUtils.hasText(baseBiz.getCreateUser()))
            criteria=criteria.and("createUser").is(baseBiz.getCreateUser());
        if(StringUtils.hasText(baseBiz.getUpdateUser()))
            criteria=criteria.and("updateUser").is(baseBiz.getUpdateUser());
        if(StringUtils.hasText(baseBiz.getRemark()))
            criteria=criteria.and("remark").regex(baseBiz.getRemark());
        if(CollectionUtil.isNotEmpty(baseBiz.getBizInfo())){
            String preKey="bizInfo.";
            //兼容key查询
            List<String> keys=CollectionUtil.newArrayList(baseBiz.getBizInfo().keySet());
            for (String key:keys){
                String value = (String) baseBiz.getBizInfo().get(key);

                criteria=criteria.and(preKey+key).is(value);
            }
        }


        Query query=new Query(criteria);
        long count = mongoTemplate.count(query, BaseBiz.class);
        PageRequest pageRequest = PageRequest.of(page - 1, size);

        query.with(pageRequest);
        Page<BaseBiz> resultPage = new PageImpl<>(mongoTemplate.find(query, BaseBiz.class), pageRequest, count);
        return resultPage;
    }
}
